package io.geekidea.springbootplus.system.controller;

import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.system.entity.SysMember;
import io.geekidea.springbootplus.system.param.sysmember.SysMemberPageParam;
import io.geekidea.springbootplus.system.param.sysuser.UploadHeadParam;
import io.geekidea.springbootplus.system.service.SysMemberService;
import io.geekidea.springbootplus.system.vo.SysMemberQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sysMember")
@Module("system")
@Api(value = "会员信息API", tags = {"会员信息"})
public class SysMemberController extends BaseController {

    @Resource
    private SysMemberService sysMemberService;

    /**
     * 系统用户分页列表
     */
    @PostMapping("/getPageList")
    @RequiresPermissions("sys:user:page")
    @OperationLog(name = "会员分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "(后端)会员分页列表", response = SysMemberQueryVo.class)
    public ApiResult<Paging<SysMemberQueryVo>> getSysMemberPageList(@Validated @RequestBody SysMemberPageParam sysMemberPageParam) throws Exception {
        Paging<SysMemberQueryVo> paging = sysMemberService.getSysMemberPageList(sysMemberPageParam);
        return ApiResult.ok(paging);
    }

    @PostMapping("/add")
    @OperationLog(name = "会员信息", type = OperationLogType.ADD)
    @ApiOperation(value = "(前端 )添加会员", response = SysMember.class)
    public ApiResult addSysMember(@RequestBody SysMember sysMember)throws Exception {
        boolean result = sysMemberService.saveSysMember(sysMember);
        System.out.println(sysMember);
        if (result){
            return ApiResult.ok("会员添加成功！");
        }else {
            return ApiResult.fail(500,"会员添加失败！");
        }
    }

    @PostMapping("/update")
    @OperationLog(name = "会员信息", type = OperationLogType.UPDATE)
    @ApiOperation(value = "(前后端)修改会员信息", response = SysMember.class)
    public ApiResult updateSysMember(@RequestBody SysMember sysMember)throws Exception {

        boolean result = sysMemberService.updateSysMember(sysMember);
        if (result){
            return ApiResult.ok("会员信息修改成功！");
        }else {
            return ApiResult.fail(500,"会员信息修改失败！");
        }
    }

    @GetMapping("/findMemberByOpenid")
    @OperationLog(name = "会员信息", type = OperationLogType.INFO)
    @ApiOperation(value = "(前端)根据openid查询会员信息", response = SysMemberQueryVo.class)
    public ApiResult getSysMemberByOpenid(String openid)throws Exception {
        SysMember sysMember = sysMemberService.getMemberByOpenid(openid);
        if (sysMember != null){
            return ApiResult.ok(sysMember);
        }else {
            return  ApiResult.fail(500,"该会员不存在或被删除");
        }
    }

    @GetMapping("/findMemberById/{id}")
    @OperationLog(name = "会员信息", type = OperationLogType.INFO)
    @ApiOperation(value = "根据id查询会员信息", response = SysMemberQueryVo.class)
    public ApiResult getSysMemberById(@PathVariable("id") Integer id)throws Exception {
        SysMemberQueryVo sysMember = sysMemberService.getMemberById(id);
        if (sysMember != null){
            return ApiResult.ok(sysMember);
        }else {
            return  ApiResult.fail(500,"该会员不存在或被删除");
        }
    }

    @GetMapping("/deleteMemberById/{id}")
    @OperationLog(name = "会员信息", type = OperationLogType.INFO)
    @ApiOperation(value = "(后端)删除会员信息", response = SysMemberQueryVo.class)
    public ApiResult deleteSysMemberById(@PathVariable("id") Integer id)throws Exception {
        boolean result = sysMemberService.deleteSysMember(id);
        if (result){
            return ApiResult.ok("删除会员成功！");
        }else {
            return  ApiResult.fail(500,"删除会员失败！");
        }
    }

    @PostMapping("/deleteBatchIds")
    @OperationLog(name = "批量删除会员信息", type = OperationLogType.PAGE)
    @ApiOperation(value = "(后端)批量删除会员信息", response = SysMember.class)
    public ApiResult<String>  deleteBatchIds(@RequestBody List<Integer> ids )throws Exception {
        boolean result = sysMemberService.deleteMemberBatchIds(ids);
        if (result){
            return ApiResult.ok("批量删除成功！");
        }else {
            return ApiResult.fail(500,"批量删除失败！");
        }
    }

}
